package main

import (
	"fmt"
	"os"
	"study/config"
	"study/dao"
	"study/log"
	"study/router"
	"time"

	"github.com/gocolly/colly"
	"github.com/gocolly/colly/debug"
	"gopkg.in/urfave/cli.v1"
)

func main() {
	// spider.SouGouHaHaSpider()

	root()
	// test()
}
func root() {
	//解析命令行
	parseCliParam()
	//初始化配置
	config.Init()
	//初始化日志
	log.Init()
	//初始化dao
	dao.Init()
	//初始化路由
	router.Init()

}
func test() {
	url := "https://httpbin.org/delay/2"

	// Instantiate default collector
	c := colly.NewCollector(
		// Attach a debugger to the collector
		colly.Debugger(&debug.LogDebugger{}),
		colly.Async(true),
	)

	// Limit the number of threads started by colly to two
	// when visiting links which domains' matches "*httpbin.*" glob
	c.Limit(&colly.LimitRule{
		DomainGlob:  "*httpbin.*",
		Parallelism: 2,
		RandomDelay: 5 * time.Second,
	})

	// Start scraping in four threads on https://httpbin.org/delay/2
	for i := 0; i < 4; i++ {
		c.Visit(fmt.Sprintf("%s?n=%d", url, i))
	}
	// Start scraping on https://httpbin.org/delay/2
	c.Visit(url)
	// Wait until threads are finished
	c.Wait()
}

/**
解析命令行参数
*/
func parseCliParam() {
	//实例化一个命令行程序
	app := cli.NewApp()
	//程序名称
	app.Name = "xiaobei"
	//程序的用途描述
	app.Usage = "parse profile"
	//程序的版本号
	app.Version = "1.0.0"

	//设置启动参数
	app.Flags = []cli.Flag{
		//参数string, int, bool
		cli.StringFlag{
			Name:  "profile, p",                                      //参数名称
			Value: "back-dev",                                        //参数默认值
			Usage: "要加载的配置文件: front-dev,front-pro,back-dev,back-pro", //参数功能描述
		},
	}
	app.Action = func(c *cli.Context) error {
		config.Config.Profile = c.String("profile") //不使用变量接收，直接解析
		return nil
	}
	app.Run(os.Args)
}
